<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
	String path = request.getContextPath(); //解決跳轉后樣式丟失的問題
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 	
%>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
   <link rel="stylesheet" type="text/css" href="<%=path%>/background/style/resource/Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="<%=path%>/background/style/resource/Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="<%=path%>/background/style/resource/Css/style.css" />
    <script type="text/javascript" src="<%=path%>/background/style/resource/Js/jquery.js"></script>
    <script type="text/javascript" src="<%=path%>/background/style/resource/Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="<%=path%>/background/style/resource/Js/bootstrap.js"></script>
    <script type="text/javascript" src="<%=path%>/background/style/resource/Js/ckform.js"></script>
    <script type="text/javascript" src="<%=path%>/background/style/resource/Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form class="form-inline definewidth m20">  
    评价ID/买家名/订单编号：
    <input type="text" name="commentname" id="commentname"class="abc input-default" placeholder="" value="">&nbsp;&nbsp;  
    <button type="button" class="btn btn-primary" onclick="selectById()">ID查询</button>&nbsp;&nbsp; 
    <button type="button" class="btn btn-primary" onclick="selectByName()">买家名查询</button>&nbsp;&nbsp; 
    <button type="button" class="btn btn-primary" onclick="selectByOid()">订单ID查询</button>&nbsp;&nbsp; 
</form>
<table class="table table-bordered table-hover definewidth m10" >
    <thead>
    <tr>
        <th>ID</th>
         <th>用户名</th>
         <th>订单号</th>
         <th>评价对象</th>
        <th>评价时间</th>
        <th>管理操作</th>
    </tr>
    </thead>
    <tbody id="tab1">	
		</tbody>
	</table>
	 ${msg }
	 <a id="pageIndex" hidden="hidden">${param.pageIndex}</a>
	<div  id="div2"></div>
</body>
</html>
<script>
var url = "/tbpoject/business4/";
var pageCount=1;
var totalpage=1;
$(function() {
	
	loadpageCount();
	if($("#pageIndex").html()==null||$("#pageIndex").html()==""){
		getAllComments(1,10);
	}else{
		getAllComments(parseInt($("#pageIndex").html()),10);
	}
	

});

function loadpageCount(){
	
	$.get(url+"getcommentscount",{},function(data){
	
		pageCount=parseInt(data);
		totalpage=Math.ceil(pageCount/10);
		
		loadPagination();
	})
} 

//通过评价id来查找评价
function selectById() {
	var id = $("#commentname").val();
	$.getJSON(url + "selectcommentbyid",
					{
						"id" : id
					},
					function(data) {
						if(data!=""&&data!=null){
						var tab = $("#tab1");
						var msg = "";
						tab.html("");
						msg += "<tr title="+data.content+">";
						msg += "<td>" + data.id + "</td>";
						msg += "<td>" + data.user.name + "</td>";
						msg += "<td>" + data.oid + "</td>";
						msg += "<td>" + data.goods.name + "</td>";
						
						msg += "<td>" + data.time + "</td>";
						msg += "<td><a href='/tbpoject/business4/getcommentbyid?id="
							+ data.id + "'>详情</a></td>";
						msg += "</tr>";
						
						tab.html(tab.html() + msg);
						}else{
							setPageHidden();
							
						}
					})
}

//根据订单id来查询他的评价
function selectByOid() {
		var oid = $("#commentname").val();
		$
				.post(
						url + "selectcommentsbyoid",
						{
							"oid" : oid
						},
						function(info) {
							
							var data = $.parseJSON(info);
							if(data!=""&&data!=null){
							var tab = $("#tab1");
							var msg = "";
							tab.html("");
							for (var i = 0; i < data.length; i++) {
								msg += "<tr title="+data[i].content+">";
								msg += "<td>" + data[i].id + "</td>";
								msg += "<td>" + data[i].user.name + "</td>";
								msg += "<td>" + data[i].oid + "</td>";
								msg += "<td>" + data[i].goods.name + "</td>";
								
								msg += "<td>" + data[i].time + "</td>";
								msg += "<td><a href='/tbpoject/business4/getcommentbyid?id="
									+ data[i].id + "'>详情</a></td>";
								msg += "</tr>";
							}
							tab.html(tab.html() + msg);
						}else{
							setPageHidden();
							
						}
						})
	}
//根据买家名来查询他的评价
function selectByName() {
		var name = $("#commentname").val();
		$
				.post(
						url + "selectcommentsbyname",
						{
							"name" : name
						},
						function(info) {
							
							var data = $.parseJSON(info);
							if(data!=""&&data!=null){
							var tab = $("#tab1");
							var msg = "";
							tab.html("");
							for (var i = 0; i < data.length; i++) {
								msg += "<tr title="+data[i].content+">";
								msg += "<td>" + data[i].id + "</td>";
								msg += "<td>" + data[i].user.name + "</td>";
								msg += "<td>" + data[i].oid + "</td>";
								msg += "<td>" + data[i].goods.name + "</td>";
								
								msg += "<td>" + data[i].time + "</td>";
								msg += "<td><a href='/tbpoject/business4/getcommentbyid?id="
									+ data[i].id + "'>详情</a></td>";
								msg += "</tr>";
							}
							tab.html(tab.html() + msg);
						}else{
							setPageHidden();
							
						}
						})
	}
//若没有数据则将分页隐藏
function setPageHidden(){
	var div = $('#div2');
	div.prop("style","display:none;");
	$("#tab1").html("无数据！");
}



function loadPagination() {
	
	var Pager = new Pagination({
		parent : '#div2',
		totalPage : totalpage,
		prevText : "上一页",
		nextText : "下一页",
		pageSize : 5
	});

	Pager.on('afterChange', function(args) {
		var index = args.currentPage
		getAllComments(index, 10);
	});
}



function getAllComments(pageIndex, pageSize) {
	$
			.getJSON(
					url + "getallcomments",
					{"pageIndex" : pageIndex,
						"pageSize" : pageSize},
					function(data) {
							if(data!=""&&data!=null){
						var tab = $("#tab1")
						var msg = "";
						tab.html("");
						for (var i = 0; i < data.length; i++) {

							msg += "<tr title="+data[i].content+">";
							msg += "<td>" + data[i].id + "</td>";
							msg += "<td>" + data[i].user.name + "</td>";
							msg += "<td>" + data[i].oid+"</td>";
							msg += "<td>" + data[i].goods.name+"</td>";
							msg += "<td>" + data[i].time+"</td>";
							msg += "<td><a href='/tbpoject/business4/getcommentbyid?id="
									+ data[i].id + "'>详情</a></td>";
							msg += "</tr>";
						}
						tab.html(tab.html() + msg);
					}else{
						setPageHidden();
						
					}
					});

}

	
</script>